package com.herench.bunch.emr.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.message.ADT_A01;
import ca.uhn.hl7v2.model.v24.message.ORM_O01;
import ca.uhn.hl7v2.model.v24.message.QRY_Q01;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luoxiaoming on 18-10-16 上午10:30
 * @email luoxiaoming@herench.com
 */
public class EmrDiagnosisConvert extends AbstractMessageConvert {

    @Override
    public Map<String,Object> doToMessage(RequestMessage requestMessage) throws Exception {
        Map<String,Object> params = new HashMap<>();
        QRY_Q01 hl7Message = (QRY_Q01) requestMessage.getHl7Message();
        QRD qrd = hl7Message.getQRD();
        String patientId = qrd.getQrd4_QueryID().getValue();
        String hisRegisterPk = qrd.getQrd1_QueryDateTime().getTs1_TimeOfAnEvent().getValue();
        params.put("patientId",patientId);
        params.put("hisRegisterPk",hisRegisterPk);
        return params;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        List<Map<String, Object>> res = (List<Map<String, Object>> ) message;
        ADT_A01 adtA01 = new ADT_A01();
        adtA01.initQuickstart("ADT_A01", "ADT_A01", "A");
        MSH msh = adtA01.getMSH();
        initResponseMessageMSH(msh);
        PID pid =  adtA01.getPID();
        Map<String,Object> reqMap = res.get(0);
        pid.getPid2_PatientID().getCx1_ID().setValue(String.valueOf(reqMap.get("hisRegisterPk")));
        pid.getPid2_PatientID().getCx2_CheckDigit().setValue(String.valueOf(reqMap.get("patientId")));


        for(int i = 0;i<res.size();i++){
            DG1 dg1 = adtA01.getDG1(i);
            Map<String,Object> resMap = res.get(i);
            dg1.getDg16_DiagnosisType().setValue(String.valueOf(resMap.get("node")));
            dg1.getDg14_DiagnosisDescription().setValue(String.valueOf(resMap.get("content")));
            dg1.getDg19_DRGApprovalIndicator().setValue(String.valueOf(resMap.get("userId")));
            dg1.getDg115_DiagnosisPriority().setValue(String.valueOf(resMap.get("isClear")));
            dg1.getDg11_SetIDDG1().setValue(String.valueOf(resMap.get("orderNo")));
            dg1.getDg13_DiagnosisCodeDG1().getCe1_Identifier().setValue(String.valueOf(resMap.get("orderId")));
            dg1.getDg118_ConfidentialIndicator().setValue(String.valueOf(resMap.get("isTable")));

        }

        return adtA01;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QRY_Q01 qry = new QRY_Q01();
        qry.initQuickstart("QRY_Q01", "Q01", "A");
        MSH msh = qry.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("300040");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("PACS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        QRD qrd = qry.getQRD();
        addQrd(qrd, "testNo", "M100610000001E");
        addQrd(qrd, "resultStatus", "2");
        addQrd(qrd, "reporter", "1503");
        addQrd(qrd, "verifiedBy", "1503");
        return qry;
    }
}
